<?php
class BorrowController extends Controller{
	
	//展示所有借借阅信息
	public function  indexAction(){
		$title="图书借还";
		$include_file="allborrow";
		$nav=$this->getNavs();
		$libraryinfo=$this->getFooter();
		$borrowmodel=new BorrowModel("tb_borrow");
		//定义每页数量
		$pagesize = 10;
		//接收页数参数,如果为空,则显示第一页
		$current = isset ( $_GET ['page'] ) ? $_GET ['page'] : 1;
		//计算偏移量
		$offset = ($current - 1) * $pagesize;
		//获取总条数,条件where为空
		$total = $borrowmodel->total("");
		$allborrows = $borrowmodel->getAllBorrow($offset, $pagesize);
		//$this->diehere($allborrows);
		//载入分页工具类,获取分页信息
		$this->library ( "Page" );
		$page=new Page($total, $pagesize, $current,"index.php",array("c"=>"borrow","a"=>"index"));
		$pageinfo=$page->showPage();
		
		include VIEWS_PATH."index.html";
	}
	
	//查询读者借阅信息
	public function selectAction(){
		$title="图书借还";
		$include_file="selectone";
		$nav=$this->getNavs();
		$libraryinfo=$this->getFooter();
		$readerbocode=$_POST['barcode']+0;
		if (empty($readerbocode)) {
			$this->jump("index.php?c=borrow&a=index", "请输入读者条形码!",2);
		}
		//查询读者信息
		$readermodel=new ReaderModel("tb_reader");
		
		$readerinfo=$readermodel->getOneReader($readerbocode);
		if (!$readerinfo) {
			$this->jump("index.php?c=borrow&a=index", "没有这个读者的相关信息,请核实!",2);
		}
		//$this->diehere($readerinfo);
		//查询该读者的未还图书
		$borrowmodel=new BorrowModel("tb_borrow");
		$borrowinfos=$borrowmodel->getOneReaderBorrow($readerinfo['id']);
		//$this->diehere($borrowinfos);
		include VIEWS_PATH."index.html";
	}
	
	
	public function selectBorrowAction(){
		$title="图书借还";
		$include_file="selectBorrow";
		$nav=$this->getNavs();
		$libraryinfo=$this->getFooter();
		//查询读者信息
		$readerbocode=isset($_POST['barcode'])?$_POST['barcode']:$_GET['rcode'];
		$readerbocode=$readerbocode+0;
		$readermodel=new ReaderModel("tb_reader");
		$readerinfo=$readermodel->getOneReader($readerbocode);
		
		//查询该读者的未还图书
		$borrowmodel=new BorrowModel("tb_borrow");
		$borrowinfos=$borrowmodel->getOneReaderBorrow($readerinfo['id']);
		//$this->diehere($borrowinfos);
		
		//查询图书信息
		if (isset($_POST['bookbarcode'])){
			$bookbocode=$_POST['bookbarcode']+0;
			$bookmodel=new BookModel("tb_bookinfo");
			$bookinfo=$bookmodel->getOneInfo($bookbocode);
		}
		//$this->diehere($bookinfo);
		include VIEWS_PATH."index.html";
	}
	
	//图书借阅(在tb_borrow表中添加数据)
	public function BorrowBookAction(){
		$data['readerid']=$_POST['readerid'];
		$data['bookid']=$_POST['bookid'];
		$data['borrowTime']=date("Y-m-d");
		$readerbarcode=$_POST['readerbarcode'];
		//查询该读者一共可以节约多少本书
		$readermodel=new ReaderModel("tb_reader");
		$readerinfo=$readermodel->getOneReader1($data['readerid']);
		$allownumber=$readerinfo['number'];
		$borrowmodel=new BorrowModel("tb_borrow");
		$where="readerid={$data['readerid']} and ifback=0";
		$total=$borrowmodel->total($where);
		//判断是否超过限定借书数量
		if ($total>=$allownumber) {
			$this->jump("index.php?c=borrow&a=selectborrow&rcode={$readerbarcode}",
			 "你最多允许同时借阅{$allownumber}本书,目前达到或超过上限,请先归还之前借的图书后重新借阅,若需要增加可借数量,请联系管理员协商解决!");
		}
		//在进行借阅之前,先验证一下读者是否已经 借阅这本书,并且没有归还
		$where1="readerid={$data['readerid']} and bookid={$data['bookid']} and ifback=0";
		if ($borrowmodel->total($where1)>=1) {
			$this->jump("index.php?c=borrow&a=selectborrow&rcode={$readerbarcode}",
			 "你不能借阅这本书,有记录显示该读者之前借阅过这本书,并且尚未归还!");
		}

		//获取该图书的最大可借天数
		$dayinfo=$borrowmodel->getBookBorrowDays($data['bookid']);
		$day=$dayinfo['days'];
		//获取归还时间
		$data['backTime']=date("Y-m-d",time()+3600*24*$day);
		$data['operator']=$_SESSION['admin_name'];
		if ($borrowmodel->insert($data)) {
			$this->jump("index.php?c=borrow&a=selectborrow&rcode={$readerbarcode}",
			 "借阅成功,借阅时长:{$day}天,归还时间:{$data['backTime']},请提示读者爱护书籍,并按时归还!",3);
		}
		
	}
	
	//图书归还(是将tb_borrow表中某条信息的ifback字段的值改为1)
	public function BackBookAction(){
		$readerbarcode=$_POST['readerbarcode']+0;
		$data['id']=$_POST['id']+0;
		$data['operator']=$_SESSION['admin_name'];
		$data['ifback']=1;
		$borrowmodel=new BorrowModel("tb_borrow");
		if ($borrowmodel->update($data)) {
			$this->jump("index.php?c=borrow&a=selectborrow&rcode={$readerbarcode}",
			"图书归还成功,谢谢!",2);
		}else{
			$this->jump("index.php?c=borrow&a=selectborrow&rcode={$readerbarcode}",
			"图书归还失败,请刷新后再试!",3);
		}
		
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}













